package service

import (
	"context"
	"diygw-ui-go/internal/v1/dao"
	"diygw-ui-go/internal/v1/model/admin"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/util/gconv"
	"time"
)

type logLogins struct{}

// 对象实例化
func LogLogins() *logLogins {
	return &logLogins{}
}

// 获取列表不分页
func (s *logLogins) GetList(ctx context.Context, in admin.LogLoginslistInput) (out admin.LogLoginslistOutput, err error) {
	m := dao.LogLogins.Ctx(ctx).OmitEmpty()
	//if len(in.RoleIds) > 0 {
	//	m = m.Where("role_id in (?)", in.RoleIds)
	//} else if in.RoleId > 0 {
	//	m = m.Where("role_id= ?", in.RoleId)
	//}
	if err = m.OrderDesc("create_time").Scan(&out.List); err != nil {
		return
	}
	out.Total, err = m.Count()
	return
}

// 获取列表分页
func (s *logLogins) GetListPage(ctx context.Context, in admin.LogLoginslistInput) (out admin.LogLoginslistOutput, err error) {
	m := dao.LogLogins.Ctx(ctx).OmitEmpty()
	//if in.RoleId > 0 {
	//	m = m.Where("role_id= ?", in.RoleId)
	//}
	if err = m.Page(in.PageNum, in.PageSize).OrderDesc("create_time").Scan(&out.List); err != nil {
		return
	}
	out.Total, err = m.Count()
	return
}

// 获取详细信息,缓存10小时
func (s *logLogins) GetOne(ctx context.Context, in admin.SysRoleOneInput) (out admin.SysRoleOneInput, err error) {
	err = dao.LogLogins.Ctx(ctx).Cache(gdb.CacheOption{
		Duration: time.Hour * 10,
		Name:     "roleID-" + gconv.String(in.RoleId),
		Force:    false,
	}).Where("role_id", in.RoleId).Scan(&out)
	return
}
